/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.yangfei.SportAssistant.fragments;

import com.yangfei.SportAssistant.R;
import com.yangfei.SportAssistant.content.SportAssistantProviderUtils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;

/**
 * A DialogFragment to delete all tracks.
 * 
 * @author Jimmy Shih
 */
/*
 * public class DeleteAllTrackDialogFragment extends AlertDialog{
 * 
 * public static final String DELETE_ALL_TRACK_DIALOG_TAG =
 * "deleteAllTrackDialog"; Context context;
 * 
 * 
 * @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return
 * DialogUtils.createConfirmationDialog(getActivity(),
 * R.string.track_list_delete_all_confirm_message, new
 * DialogInterface.OnClickListener() {
 * 
 * @Override public void onClick(DialogInterface dialog, int which) {
 * SportAssistantProviderUtils.Factory.get(getActivity()).deleteAllTracks(); }
 * }); }
 * 
 * public DeleteAllTrackDialogFragment(Context context) { super(context);
 * this.context = context; }
 * 
 * public static DeleteOneTrackDialogFragment newInstance(long trackId) { Bundle
 * bundle = new Bundle(); bundle.putLong(KEY_TRACK_ID, trackId);
 * 
 * DeleteOneTrackDialogFragment deleteTrackDialogFragment = new
 * DeleteOneTrackDialogFragment();
 * deleteTrackDialogFragment.setArguments(bundle); return
 * deleteTrackDialogFragment; }
 * 
 * @Override protected void onCreate(Bundle savedInstanceState) {
 * super.onCreate(savedInstanceState);
 * 
 * AlertDialog.Builder builder = new AlertDialog.Builder(context);
 * builder.setMessage(
 * context.getString(R.string.track_list_delete_all_confirm_message));
 * builder.setTitle(context.getString(R.string.are_you_sure_question));
 * builder.setIcon(android.R.drawable.ic_dialog_alert);
 * builder.setPositiveButton(context.getString(R.string.generic_ok), new
 * DialogInterface.OnClickListener() { public void onClick(DialogInterface
 * dialog, int i) { dialog.dismiss();
 * SportAssistantProviderUtils.Factory.get(context).deleteAllTracks();
 * SharedPreferences prefs =
 * context.getSharedPreferences(SportAssistantSettings.SETTINGS_NAME, 0);
 * SharedPreferences.Editor editor = prefs.edit();
 * editor.putLong(SportAssistantSettings.SELECTED_TRACK, -1); editor.commit(); }
 * }); builder.setNegativeButton(context.getString(R.string.generic_no), new
 * DialogInterface.OnClickListener() { public void onClick(DialogInterface
 * dialog, int i) { dialog.dismiss(); } }); builder.create(); show(); } }
 */

public class DeleteAllTrackDialogFragment extends Handler
{

	private final Context context;
	private final Runnable done;

	public DeleteAllTrackDialogFragment(Context context, Runnable done)
	{
		this.context = context;
		this.done = done;
	}

	@Override
	public void handleMessage(Message msg)
	{
		super.handleMessage(msg);

		AlertDialog dialog = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(context.getString(R.string.track_list_delete_all_confirm_message));
		builder.setTitle(context.getString(R.string.are_you_sure_question));
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setPositiveButton(context.getString(R.string.generic_yes), new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int i)
			{
				dialog.dismiss();
				SportAssistantProviderUtils.Factory.get(context).deleteAllTracks();
				if (done != null)
				{
					Handler h = new Handler();
					h.post(done);
				}
			}
		});
		builder.setNegativeButton(context.getString(R.string.generic_no), new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int i)
			{
				dialog.dismiss();
			}
		});
		dialog = builder.create();
		dialog.show();
	}
}